package com.wyjy.bkd.ui.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.wyjy.bkd.R;
import com.wyjy.bkd.base.BaseActivity;
import com.wyjy.bkd.ui.fragment.ExpressFragment;
import com.wyjy.bkd.ui.fragment.FamilyFragment;
import com.wyjy.bkd.ui.fragment.FindFragment;
import com.wyjy.bkd.ui.fragment.HomeFragment;
import com.wyjy.bkd.ui.fragment.MeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {


    private FragmentManager mManager;
    //fragment集合
    private List<Fragment>  mFragmentList;
    //fragment tag集合
    private String[] mFragmentTags = new String[]{"home", "family", "express", "find", "me"};
    //当前fragment index
    private int      mCurrentTab   = 0;
    //退出点击时间
    private long     mOldTime      = 0;

    private Fragment   mHomeFragment;
    private Fragment   mFamilyFragment;
    private Fragment   mExpressFragment;
    private Fragment   mFindFragment;
    private Fragment   mMeFragment;
    private RadioGroup mRgAppMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mManager = getSupportFragmentManager();
        initFragment(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void initView() {
        mRgAppMenu = (RadioGroup) findViewById(R.id.rg_app_menu);
        mRgAppMenu.setOnCheckedChangeListener(this);
        ((RadioButton) mRgAppMenu.getChildAt(0)).setChecked(true);
    }


    /**
     * 初始化fmt
     *
     * @param savedInstanceState
     */
    private void initFragment(Bundle savedInstanceState) {
        mFragmentList = new ArrayList<>();
        if (savedInstanceState != null) {/*如果bundle存有fragment对象，则复用*/
            mHomeFragment = mManager.findFragmentByTag("HomePage");
            mFamilyFragment = mManager.findFragmentByTag("Categray");
            mExpressFragment = mManager.findFragmentByTag("Find");
            mFindFragment = mManager.findFragmentByTag("Cart");
            mMeFragment = mManager.findFragmentByTag("Me");
        } else {
            mHomeFragment = new HomeFragment();
            mFamilyFragment = new FamilyFragment();
            mExpressFragment = new ExpressFragment();
            mFindFragment = new FindFragment();
            mMeFragment = new MeFragment();
        }
        addFragmentToList();
    }


    /**
     * 添加fragment到list
     */
    private void addFragmentToList() {
        mFragmentList.add(mHomeFragment);
        mFragmentList.add(mFamilyFragment);
        mFragmentList.add(mExpressFragment);
        mFragmentList.add(mFindFragment);
        mFragmentList.add(mMeFragment);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        for (int i = 0; i < mFragmentList.size(); i++) {
            if (mRgAppMenu.getChildAt(i).getId() == checkedId) {
                Fragment            fragment = mFragmentList.get(i);
                FragmentTransaction ft       = obtainFragmentTransaction();
                if (getCurrentFragment() != null && getCurrentFragment().isAdded()) {
                    getCurrentFragment().onPause();
                }
                if (fragment.isAdded()) {
                    //fragment.onStart();
                } else {
                    ft.add(R.id.fragment_container, fragment, mFragmentTags[i]);
                    ft.commit();
                }
                showTab(i);
            }
        }
    }

    private FragmentTransaction obtainFragmentTransaction() {
        FragmentTransaction ft = mManager.beginTransaction();
        return ft;
    }

    private Fragment getCurrentFragment() {
        return mFragmentList.get(mCurrentTab);
    }


    /**
     * 切换tab
     *
     * @param index
     */
    private void showTab(int index) {
        for (int i = 0; i < mFragmentList.size(); i++) {
            Fragment            fragment = mFragmentList.get(i);
            FragmentTransaction ft       = obtainFragmentTransaction();
            ft.hide(fragment);
            ft.commit();
        }
        obtainFragmentTransaction().show(mFragmentList.get(index)).commit();
        mCurrentTab = index; // 更新目标tab为当前tab
    }


    //    /**
//     * 双击退出
//     */
//    @Override
//    public void onBackPressed() {
//        if (System.currentTimeMillis() - mOldTime < 3000) {
//            super.onBackPressed();
//            System.exit(0);
//            return;
//        }
//        mOldTime = System.currentTimeMillis();
//        showToast(getString(R.string.press_again_to_exit_app));
//    }


}
